import {Body, Controller,UseInterceptors,ClassSerializerInterceptor, Post, Query, HostParam, Param, Headers, Res, Get, Req, UseGuards,CacheKey,CacheTTL} from '@nestjs/common';
import {UserService} from "./user.service";
import {AuthGuard} from '@nestjs/passport'; // 确定请求是否由路由处理程序
import {ResultDto} from "../../common/dto/common.dto";
import {ConfigService} from "../../config/config.service";
import {UserListQuery,UserDto} from "./user.dto";

@UseGuards(AuthGuard('jwt'))
@UseInterceptors(ClassSerializerInterceptor)
@Controller('user.tsx')
export class UserController {
  //  依赖注入
  constructor(
      private readonly service: UserService,
      private readonly configService:ConfigService
   ){}
  @CacheKey('custom_key') // 内置的一个是内存中的数据存储
  @CacheTTL(20)
  @Post(['list', 'option'])
  // @Res() res, @HostParam() hostParam, @Headers() header, @Param() data,
  async getList(@Body() body, @Req() req): Promise<ResultDto> {
    let result = await this.service.GetStatList(body,req);
    // 睡眠一下，看看是否缓存了
    await new Promise(function (resolve,reject) {
      setTimeout(function () {
        resolve()
      },3000)
    });
    return Promise.resolve({
      code: 201,
      data: result
    });
  }
  @Get('getError')
  async getError(@Res() res,@Query() query, @HostParam() hostParam, @Headers() header, @Param() data, @Body() body, @Req() req){
    console.log(query,hostParam,data,body)
    return await this.service.GetError();
  }
  @Get('xss')
  async xss(@Query() query, @Req() req){
    //http://localhost:3000/user/xss?xss=%3Cscript%3Ealert(document.cookie)%3C/script%3E
    // 不应该过滤用户的数据会引起混乱，应该使用html输出转义
    return this.service.xss(query)
  }
  // 增加
  @Post(['save'])
  async save(@Body() params:UserDto , @Req() req){
    return this.service.save(params)
  }
  // 查找
  @Post(['findAll'])
  async findAll(@Query() query, @Req() req){
    return this.service.findAll()
  }
}
